package cn.com.infcn.monitor.task.impl;

import java.util.HashMap;
import java.util.Map;

import org.apache.mina.core.session.IoSession;
import org.hyperic.sigar.SigarException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.com.infcn.monitor.bean.IFCResponseBean;
import cn.com.infcn.monitor.util.IFCMinitorConstanst;

/**
 * 获取当前程序的jvm信息
 * 
 * @author jjs
 *
 */
public class JvmTask extends BaseTask {

	private final static Logger logger = LoggerFactory.getLogger(JvmTask.class);

	public JvmTask(IoSession session, Object message) {
		super(session, message);
	}

	@Override
	public void execute() throws Exception {
		IFCResponseBean result = jvm();
		sendResponse(result);
	}

	private IFCResponseBean jvm() throws SigarException {
		IFCResponseBean result = new IFCResponseBean();

		try {
			Runtime r = Runtime.getRuntime();

			Map<String, Object> map = new HashMap<String, Object>();
			map.put("totalMemory", r.totalMemory()); // JVM可以使用的总内存
			map.put("freeMemory", r.freeMemory()); // JVM可以使用的剩余内存
			map.put("availableProcessors", r.availableProcessors()); // JVM可以使用的处理器个数
			map.put("maxMemory", r.maxMemory());

			result.setResult(map);
			result.setResponseCode(IFCMinitorConstanst.MONITOR_RESPONSE_CODE_OK);
		} catch (Exception e) {
			logger.error(e.getMessage());
			result.setResponseCode(IFCMinitorConstanst.MONITOR_RESPONSE_CODE_ERROR);
			result.setResponseMsg(e.getMessage());
		}

		return result;
	}

}
